package net.kehuilai.tools;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by andy,genghz on 6/22/15.
 */
public final class DateTool {

    private static final Map<String, SimpleDateFormat> DATE_FORMAT_HOLDER = new ConcurrentHashMap<String, SimpleDateFormat>();
    private static final Map<String, DecimalFormat> NUMBERIC_FORMAT_HOLDER = new ConcurrentHashMap<String, DecimalFormat>();

    // -------------------------------- format str --------------------------------
    // 当前时间 格式为指定 格式
    public static String formatTime(String format) {
        return formatTime(System.currentTimeMillis(), format);
    }

    // Date 格式为指定 yyyy-MM-dd
    public static String formatDate(Date date) {
        if (date == null)
            date = new Date();
        return formatTime(date.getTime(), "yyyy-MM-dd");
    }

    // 指定时间 格式为指定 格式
    public static String formatTime(long ts, String format) {
        if (!DATE_FORMAT_HOLDER.containsKey(format)) {
            DATE_FORMAT_HOLDER.put(format, new SimpleDateFormat(format));
        }
        return DATE_FORMAT_HOLDER.get(format).format(ts);
    }

    // num 格式化指定 格式
    public static String formatNumber(double num, String format) {
        if (!NUMBERIC_FORMAT_HOLDER.containsKey(format)) {
            NUMBERIC_FORMAT_HOLDER.put(format, new DecimalFormat(format));
        }
        return NUMBERIC_FORMAT_HOLDER.get(format).format(num);
    }

    // -------------------------------- get zero time --------------------------------
    // 昨天 零时
    public static long getYesterdayTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        return getFormatDate(cal.getTimeInMillis()).getTime();
    }

    // 今天 零时
    public static long getTodayTime() {
        return getFormatDate(System.currentTimeMillis()).getTime();
    }

    // 明天 零时
    public static long getTomorrowTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        return getFormatDate(cal.getTimeInMillis()).getTime();
    }

    // 后天 零时
    public static long getAfterTomorrowTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 2);
        return getFormatDate(cal.getTimeInMillis()).getTime();
    }

    // -------------------------------- time opt --------------------------------
    // 获取当前 秒值
    public static int getCurrentSecond() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    // 当前时间 - 指定时间 的毫秒差值
    public static long diffCurrentTime(long timestamp) {
        return System.currentTimeMillis() - timestamp;
    }

    // 格式化为: (今日|明日|xx月xx日) xx(h):xx(m)
    public static String prettyDateTime(long timeStamp) {
        String pretty = "";

        if (timeStamp <= 0) {
            timeStamp = new Date().getTime();
        }

        long todayZeroTime = DateTool.getTodayTime();
        long tomorrowZeroTime = DateTool.getTomorrowTime();
        long afterTomorrowZeroTime = DateTool.getAfterTomorrowTime();

        if (timeStamp >= todayZeroTime && timeStamp < tomorrowZeroTime) {
            pretty = "今日";
        } else if (timeStamp >= tomorrowZeroTime && timeStamp < afterTomorrowZeroTime) {
            pretty = "明日";
        } else {
            pretty = "MM月dd日";
        }
        String dateFormat = pretty + " HH:mm";

        pretty = DateTool.formatTime(timeStamp, dateFormat);

        return pretty;
    }

    // -------------------------------- base --------------------------------
    // Date(yyyy-MM-dd hour:minute:second)
    public static Date getToday(int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        return cal.getTime();
    }

    // Date(yyyy-MM-dd 00:00:00)
    public static Date getFormatDate(long timestamp) {
        String format = "yyyy-MM-dd";
        String formatTime = formatTime(timestamp, "yyyy-MM-dd");
        try {
            return DATE_FORMAT_HOLDER.get(format).parse(formatTime);
        } catch (ParseException e) {
            return new Date();
        }
    }

    // 转换 yyyy-MM-dd hh:mm:ss 字符串为时间戳
    public static long getTimestamp(String dateStr) {
        Date date = null;
        if (StringTool.isEmpty(dateStr))
            return 0;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException ignore) {
        }

        return date == null ? 0 : date.getTime();
    }
}
